import { PrismaClient } from "@prisma/client";
import { NextFunction, Request, Response } from "express";

const prisma = new PrismaClient();
//系统通知处理
export const getNotifications = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  try {
    const uid = Number((req as any).user.id);
    const list = await prisma.notifications.findMany({
      where: { user_id: uid },
      orderBy: { created_at: "desc" },
      take: 20,
    });
    res.safeJson(list);
  } catch (err) {
    next(err);
  }
};

export const markRead = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  try {
    const uid = Number((req as any).user.id);
    const id = Number(req.params.id);
    const result = await prisma.notifications.updateMany({
      where: { id, user_id: uid },
      data: { status: 1 },
    });
    res.safeJson(result.count > 0 ? null : undefined, "已读成功");
  } catch (err) {
    next(err);
  }
};
